package smtp;

import java.util.regex.*;

public class MIMEutility {

    static String data;
    final static String CRLF = "\r\n";

    public MIMEutility() {
    }

    public static String MIMEEncoder(String data) {
       // data = chainCut(data);
        Pattern patron9 = Pattern.compile("=");
        Matcher encaja9 = patron9.matcher(data);
        String encodedData = encaja9.replaceAll("=3D");

        Pattern patron = Pattern.compile("å");
        // we create the Matcher from the pattern, being "data" as argument
        Matcher encaja = patron.matcher(encodedData);
        encodedData = encaja.replaceAll("=E5");

        Pattern patron2 = Pattern.compile("ä");
        Matcher encaja2 = patron2.matcher(encodedData);
        encodedData = encaja2.replaceAll("=E4");

        Pattern patron3 = Pattern.compile("ö");
        Matcher encaja3 = patron3.matcher(encodedData);
        encodedData = encaja3.replaceAll("=F6");

        Pattern patron4 = Pattern.compile("Ä");
        Matcher encaja4 = patron4.matcher(encodedData);
        encodedData = encaja4.replaceAll("=C4");

        Pattern patron5 = Pattern.compile("Ö");
        Matcher encaja5 = patron5.matcher(encodedData);
        encodedData = encaja5.replaceAll("=D6");

        Pattern patron6 = Pattern.compile("Å");
        Matcher encaja6 = patron6.matcher(encodedData);
        encodedData = encaja6.replaceAll("=C5");

        Pattern patron7 = Pattern.compile(" ");
        Matcher encaja7 = patron7.matcher(encodedData);
        encodedData = encaja7.replaceAll("=20");

         /* THIS IS STILL NOT WORKING
        Pattern patron8 = Pattern.compile(".");
        Matcher encaja8 = patron8.matcher(encodedData);
        encodedData = encaja2.replaceAll("=2E");
         */
        /* using pattern and Matcher for "." character is not working
         * Thus we do brute force here
         */
        while (true) {
            if (!encodedData.contains(".")) {
                break;
            } else {
                int i = encodedData.indexOf(".");
                if (i == 0) {
                    encodedData = encodedData.substring(1);
                    encodedData = "=2E" + encodedData;
                } else if (i == encodedData.length() - 1) {
                    encodedData = encodedData.substring(0, encodedData.length() - 1);
                    encodedData = encodedData + "=2E";
                } else {
                    encodedData = encodedData.substring(0, i) + "=2E" + encodedData.substring(i + 1, encodedData.length());
                }
            }
        }

        return encodedData;//chainCut(encodedData);
    }

    public static String MIMEEncodingSubject(String data) {
        String encodedData = MIMEEncoder(data);
        while (true) {
            if (!encodedData.contains("?")) {
                break;
            } else {
                int i = encodedData.indexOf("?");
                if (i == 0) {
                    encodedData = encodedData.substring(1);
                    encodedData = "=3F" + encodedData;
                } else if (i == encodedData.length() - 1) {
                    encodedData = encodedData.substring(0, encodedData.length() - 1);
                    encodedData = encodedData + "=3F";
                } else {
                    encodedData = encodedData.substring(0, i) + "=3F" + encodedData.substring(i + 1, encodedData.length());
                }
            }
        }
        return encodedData;
    }

    public static String MIMEDecodingSubject(String data) {
        String decodedData = MIMEDecoder(data);
        while (true) {
            if (!decodedData.contains("=3F")) {
                break;
            } else {
                int i = decodedData.indexOf("=3F");
                if (i == 0) {
                    decodedData = decodedData.substring(3);
                    decodedData = "?" + decodedData;
                } else if (i == decodedData.length() - 3) {
                    decodedData = decodedData.substring(0, decodedData.length() - 3);
                    decodedData = decodedData + "?";
                } else {
                    decodedData = decodedData.substring(0, i) + decodedData.substring(i + 3, decodedData.length());
                }
            }
        }
        return decodedData;
    }

    public static String MIMEDecoder(String data) {
        Pattern patron = Pattern.compile("=E5");
        // we create the Matcher from the pattern, being "data" as argument
        Matcher encaja = patron.matcher(data);
        // method replaceAll
        data = encaja.replaceAll("å");

        Pattern patron2 = Pattern.compile("=E4");
        Matcher encaja2 = patron2.matcher(data);
        data = encaja2.replaceAll("ä");

        Pattern patron3 = Pattern.compile("=F6");
        Matcher encaja3 = patron3.matcher(data);
        data = encaja3.replaceAll("ö");

        Pattern patron4 = Pattern.compile("=C4");
        Matcher encaja4 = patron4.matcher(data);
        data = encaja4.replaceAll("Ä");

        Pattern patron5 = Pattern.compile("=D6");
        Matcher encaja5 = patron5.matcher(data);
        data = encaja5.replaceAll("Ö");

        Pattern patron6 = Pattern.compile("=C5");
        Matcher encaja6 = patron6.matcher(data);
        data = encaja6.replaceAll("Å");

        Pattern patron7 = Pattern.compile("=20");
        Matcher encaja7 = patron7.matcher(data);
        data = encaja7.replaceAll(" ");

        Pattern patron9 = Pattern.compile("=3D");
        Matcher encaja9 = patron9.matcher(data);
        data = encaja9.replaceAll("=");

        Pattern patron8 = Pattern.compile("=2E");
        Matcher encaja8 = patron8.matcher(data);
        data = encaja2.replaceAll(".");

        return data;
    }

    //method to fix long texts into the maximum of 75 characters period
    public static String chainCut(String data) {
        int cicles = 0;
        String[] subs = new String[0];
        String cutData = "";
        if (data.length() > 65) {
            cicles = (int) data.length() / 65;
            subs = new String[cicles + 1];
            for (int i = 0; i < (cicles + 1); i++) {
                if (i == 0) {
                    if (data.length() > (65 * (i + 1))) {
                        subs[i] = data.substring(65 * i, 65 * (i + 1) - 1);
                    } else {
                        subs[i] = data.substring(65 * i, data.length());
                    }
                } else {
                    if (data.length() > (65 * (i + 1))) {
                        subs[i] = data.substring(65 * i - 1, 65 * (i + 1) - 1);
                    } else {
                        subs[i] = data.substring(65 * i - 1, data.length());
                    }
                }
                if (i == cicles) {
                    cutData = cutData + subs[cicles];
                } else {
                    cutData = cutData + subs[i] + "=" + CRLF;
                }

            }//for ending
        } else {
            /* This is to remedy bug when the character is less than 65
             * before it return empty "" string
             */
            cutData = data;
        }//if ending
        return cutData;
    }
}
